#include "head_c.h"

int WRITE_LOG(const LOG_GRADE_t log_grand, char* remark) {
    
    char log_grand_global[100] = "\0";
    getparameter("log_grand_global", log_grand_global);
    //printf("log_grand_global is = %s\n", log_grand_global);
    
    time_t now_time;
    time(&now_time);

    //最低等级DEBUG
    if((log_grand == LOG_DEBUG ||
       log_grand == LOG_INFO ||
       log_grand == LOG_WARNINGS ||
       log_grand == LOG_ERROR) &&
       strcmp(log_grand_global, "LOG_DEBUG") == 0
       ) {
            char buffer[200] = "\0";
            sprintf(buffer, "%s %s %s %d %s \n",ctime(&now_time), __FILE__, __FUNCTION__, __LINE__, remark);
            ssize_t len = strlen(buffer);
            int log_fd = open("client_log.txt", O_RDWR | O_APPEND | O_CREAT, 0666);
            write(log_fd, buffer, len);
            close(log_fd);
    }
    //等级为INFO
    else if((log_grand == LOG_INFO ||
            log_grand == LOG_WARNINGS ||
            log_grand == LOG_ERROR) &&
            strcmp(log_grand_global,"LOG_INFO") == 0
            ) {
            char buffer[200] = "\0";
            sprintf(buffer, "%s %s %s %d %s \n",ctime(&now_time), __FILE__, __FUNCTION__, __LINE__, remark);
            ssize_t len = strlen(buffer);
            int log_fd = open("client_log.txt", O_RDWR | O_APPEND | O_CREAT, 0666);
            write(log_fd, buffer, len);
            close(log_fd);
    }
    else if((log_grand == LOG_WARNINGS ||
            log_grand == LOG_ERROR) &&
       strcmp(log_grand_global,"LOG_WARNINGS") == 0
            ) {
            char buffer[200] = "\0";
            sprintf(buffer, "%s %s %s %d %s \n",ctime(&now_time), __FILE__, __FUNCTION__, __LINE__, remark);
            ssize_t len = strlen(buffer);
            int log_fd = open("client_log.txt", O_RDWR | O_APPEND | O_CREAT, 0666);
            write(log_fd, buffer, len);
            close(log_fd);
    }
    else if(log_grand == LOG_ERROR && strcmp(log_grand_global,"LOG_ERROR") == 0) {
            char buffer[200] = "\0";
            sprintf(buffer, "%s %s %s %d %s \n",ctime(&now_time), __FILE__, __FUNCTION__, __LINE__, remark);
            ssize_t len = strlen(buffer);
            int log_fd = open("client_log.txt", O_RDWR | O_APPEND | O_CREAT, 0666);
            write(log_fd, buffer, len);
            close(log_fd);
    }
    else{
        printf("error:%s %s %s %d WRITE_LOG paramenter error\n",ctime(&now_time), __FILE__, __FUNCTION__, __LINE__);
        return -1;
    }

    return 0;
}
